package ro.prjmanager.core.list;


public class ListSqlParser {
    private static String SQL_JOIN="left join";
    private static String SQL_WHERE="where";
    private static String SQL_ORDER="order";

    public synchronized static SQL parseSqlListParameter(String sql){
        SQL ret=new SQL();

        boolean join= sql.indexOf(SQL_JOIN)>=0;
        boolean where= sql.indexOf(SQL_WHERE)>=0;
        boolean order= sql.indexOf(SQL_ORDER)>=0;

        if(join&&where&&order){
            ret.setBeforeWhereParent(sql.substring(sql.indexOf(SQL_JOIN),sql.indexOf(SQL_WHERE)));
            ret.setAfterWhereParent(sql.substring(sql.indexOf(SQL_WHERE)).replace("where","and"));
        }else if(join&&order){
           ret.setBeforeWhereParent(sql.substring(sql.indexOf(SQL_JOIN),sql.indexOf(SQL_ORDER)));
           ret.setAfterWhereParent(sql.substring(sql.indexOf(SQL_ORDER)));
        }else if(where&&order){
            ret.setBeforeWhereParent("");
            ret.setAfterWhereParent(sql.substring(sql.indexOf(SQL_WHERE)).replace("where","and"));
        }else if(order){
           ret.setBeforeWhereParent("");
           ret.setAfterWhereParent(sql);
        }

        return ret;
    }
}
